package com.string.actortv.activity.qmmk;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import com.bumptech.glide.Glide;
import com.string.actortv.R;
import com.string.actortv.dto.MovieTypeList;
import com.string.actortv.util.QmmkUtil;
import com.string.actortv.util.UIHelper;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;

public class QmmkTypeActivity extends Activity {

    private Context mContext;
    private SimpleAdapter gridAdapter;
    private GridView gridView;
    private View selectedView;
    private ArrayList<HashMap<String, Object>> items;
    private String action;

    private final Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NotNull Message msg) {
            switch (msg.what) {
                default:
                    if (gridAdapter != null) {
                        gridAdapter.notifyDataSetChanged();
                    }
            }
            return true;
        }
    });

    // Activity OnCreate
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_season_list);
        mContext = this;
        this.newInit();
    }

    private void loadSeasonList() {
        UIHelper.showDialogForLoading(mContext, "正在加载...");
        QmmkUtil.getMovieTypeAsync(movieTypeList -> {
            for (MovieTypeList mtl : movieTypeList) {
                HashMap<String, Object> item = new HashMap<>();
                item.put("title", mtl.getName());
                item.put("icon", "https://www.jxtcd.com/favicon.ico");
                item.put("type", mtl.getHref());

                items.add(item);
            }

            handler.sendEmptyMessage(0);
            UIHelper.dismiss();
        });
    }

    // initiate new UI content
    private void newInit() {
        items = new ArrayList<>();
        this.gridView = this.findViewById(R.id.item_grid);
        if (gridView == null) {
            throw new IllegalArgumentException("the gridView is null");
        }

        //异步加载数据
        loadSeasonList();
        this.gridAdapter = new SimpleAdapter(this, items,
                R.layout.function_block, new String[]{"title", "icon"},
                new int[]{R.id.Item_text, R.id.Item_bt});
        gridAdapter.setViewBinder((view, data, textRepresentation) -> {
            //判断是否为我们要处理的对象
            if (view instanceof ImageView && data instanceof String) {
                ImageView iv = (ImageView) view;
                Glide.with(this).load(data.toString()).error(R.drawable.no_pic).into(iv);
                return true;
            } else {
                return false;
            }
        });
        this.gridView.setAdapter(gridAdapter);
        this.gridAdapter.notifyDataSetChanged();

        this.gridView.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                if (selectedView != null) {
                    selectedView.setBackgroundColor(Color.WHITE);
                }
                View first = gridView.getChildAt(0);
                if (first != null) {
                    first.setBackgroundColor(Color.GRAY);
                    selectedView = first;
                }
            } else {
                if (selectedView != null) {
                    selectedView.setBackgroundColor(Color.WHITE);
                    selectedView = null;
                }
            }
        });

        this.gridView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (selectedView != null) {
                    selectedView.setBackgroundColor(Color.WHITE);
                }
                if (view != null) {
                    view.setBackgroundColor(Color.GRAY);
                    selectedView = view;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                if (selectedView != null) {
                    selectedView.setBackgroundColor(Color.WHITE);
                    selectedView = null;
                }
            }

        });

        this.gridView.setOnItemClickListener((gridView, view, position, id) -> {
            Intent intent1 = new Intent(QmmkTypeActivity.this,
                    QmmkActivity.class).setAction(action);
            intent1.putExtra("type", items.get(position).get("type").toString());
            startActivity(intent1);
        });
    }

}
